<template>
  <div>
    <el-form
      ref="addFormRef"
      :rules="rules"
      :model="addForm.data"
      label-width="160"
    >
      <el-row>
        <el-form-item label="收费员姓名" prop="realName">
          <el-input
            v-model="addForm.data.realName"
            style="width: 320px"
            minlength="2"
            maxlength="10"
            placeholder="请输入收费员姓名"
          />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="addForm.data.sex">
            <el-radio
              v-for="item in sex.data"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="收费员账号">
          <el-input
            style="width: 320px"
            placeholder="由系统自动生成"
            disabled
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="addForm.data.phone"
            style="width: 320px"
            maxlength="11"
            placeholder="请输入手机号码"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="紧急联系人" prop="emergency">
          <el-input
            v-model="addForm.data.emergency"
            placeholder="请输入紧急联系人"
            style="width: 320px"
          />
        </el-form-item>
        <el-form-item label="紧急联系人手机号码" prop="emergencyPhone">
          <el-input
            v-model="addForm.data.emergencyPhone"
            placeholder="请输入紧急联系人手机号码"
            style="width: 320px"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="备注">
          <el-input
            v-model="addForm.data.remark"
            placeholder="请输入备注"
            style="width: 320px"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)"
              >重 置</el-button
            >
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { getAddCashier } from "../request.js";

const sex = reactive({
  data: [
    {
      id: 0,
      name: "女",
    },
    {
      id: 1,
      name: "男",
    },
  ],
});

const addFormRef = ref("");

const addForm = reactive({
  data: {
    realName: "",
    sex: "",
    phone: "",
    emergency: "",
    emergencyPhone: "",
    remark: "",
  },
});

const rules = reactive({
  realName: [
    {
      required: true,
      message: "请输入收费员姓名",
      trigger: "blur",
    },
    {
      min: 2,
      max: 10,
      message: "长度在 2 到 10 个字符",
      trigger: "blur",
    },
  ],
  sex: [
    {
      required: true,
      message: "请选择性别",
      trigger: "change",
    },
  ],
  phone: [
    {
      required: true,
      message: "请输入手机号码",
      trigger: "blur",
    },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "手机号码格式不正确，请重新输入",
      trigger: "blur",
    },
  ],
  emergency: [
    {
      required: true,
      message: "请输入紧急联系人",
      trigger: "blur",
    },
  ],
  emergencyPhone: [
    {
      required: true,
      message: "请输入紧急联系人手机号码",
      trigger: "blur",
    },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "手机号码格式不正确，请重新输入",
      trigger: "blur",
    },
  ],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getAddCashier(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  addForm.data = {};
  formEl.resetFields();
};

const emits = defineEmits(["operationDone"]);
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>